package com.loc.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loc.dao.MenusDao;
import com.loc.dao.RolesDao;

@Controller
@RequestMapping(value = "/role")
public class RoleService extends BaseService{

	@Autowired
	private MenusDao menuDao;
	@Autowired
	private RolesDao roleDao;
	
	@RequestMapping(value = "/getRolesByParam.json", method = RequestMethod.POST)
	public @ResponseBody Map getRoles(HttpServletRequest request, HttpSession session) {
		UserDetails userDetails = null;
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) {
			userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		} else {
			map.put("failture", true);
			map.put("msg", "没有权限!");
			return map;
		}
		String rp = request.getParameter("rp");
		String page = request.getParameter("page");
		String roleName = request.getParameter("role_name");
		String issys = request.getParameter("issys");
		int startRow = 0;
		int rowsCount = 20;
		if (rp != null && !"".equals(rp)) {
			rowsCount = Integer.valueOf(rp);
		}
		if (page != null && !"".equals(page)) {
			startRow = (Integer.valueOf(page) - 1) * rowsCount;
		}
		try {
			list = menuDao.findMenuByUser(userDetails);
			//List returnList = tree(list, 0, 0);
			map.put("success", true);
			//map.put("msg", returnList);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("failture", true);
			map.put("msg", "系统出错!请尝试刷新页面!");
		}
		return map;
	}
}
